//
//  ViewController.m
//  details(个人详情页效果)
//
//  Created by 李松 on 2017/11/9.
//  Copyright © 2017年 李松. All rights reserved.
//

#import "ViewController.h"
#import "UINavigationController+alpha.h"
#import "UIImage+colorImg.h"

#define ORIOFSETY  -260
#define ORIHEIGHT 200
#define NAVH  ([[UIScreen mainScreen] bounds].size.height == 812.0f ? 88.0f : 64.0f)

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;

@end

static NSString *ID = @"cell";

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    self.tableView.contentInset = UIEdgeInsetsMake(260, 0, 0, 0);
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"个人详情页";
    label.alpha = 0;
    [label sizeToFit];
    self.navigationItem.titleView = label;
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

// 获取子视图
- (void)getSub:(UIView *)view andLevel:(int)level {
    NSArray *subviews = [view subviews];
    if ([subviews count] == 0) return;
    for (UIView *subview in subviews) {
        
        NSString *blank = @"";
        for (int i = 1; i < level; i++) {
            blank = [NSString stringWithFormat:@"  %@", blank];
        }
        NSLog(@"%@%d: %@", blank, level, subview.class);
        [self getSub:subview andLevel:(level+1)];
        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld个", indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self performSegueWithIdentifier:@"setting" sender:self];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat ofset = scrollView.contentOffset.y - ORIOFSETY;
    CGFloat h = ORIHEIGHT - ofset;
    if (h < NAVH) {
        h = NAVH;
    }
    CGFloat alpha = ofset / (ORIHEIGHT - NAVH);
    if (alpha >= 1) {
        alpha = 0.99;
    }
    UIColor *color = [UIColor colorWithWhite:1 alpha:alpha];
    UIImage *image = [UIImage createImageWithColor:color];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.viewHeight.constant = h;
    self.navigationItem.titleView.alpha = alpha;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
